package com.wskeee.ec.components.list
{
	import com.wskeee.ec.containers.VBox;
	
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;

	public class List extends ListBase
	{
		protected var listDatas:Dictionary = new Dictionary(true);
		
		public function List()
		{
			super();
			itemRenderer = ListDefaultRenderer;
		}
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		override public function set dataProvider(value:Object):void
		{
			super.dataProvider = value;
			if(dataProvider)
				createListData();
		}
		//--------------------------------------------------------------------------
		//
		//  private Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  listdata
		//-----------------------------------------------------------------
		protected function createListData():void
		{
			for(var z:Object in listDatas)
				delete listDatas[z];
			for(var i:int=0,len:int=dataProvider.length;i<len;i++)
			{
				listDatas[dataProvider[i]] = new BaseListData(dataProvider[i][labelField],this);
			}
		}
		/**
		 * 添加一项 
		 * @param item
		 * 
		 */
		protected function addListData(item:Object):void
		{
			listDatas[item] = new BaseListData(item[labelField],this);
		}
		protected function removeListData(item:Object):void
		{
			delete listDatas[item];
		}
		/**
		 * 通过 item data 返回 listData 
		 * @param data
		 * @return 
		 * 
		 */
		protected function getListData(data:Object):BaseListData
		{
			return listDatas[data];
		}
		//-----------------------------------------------------------------
		//  renderBox
		//-----------------------------------------------------------------
		override protected function initRenderBox():void
		{
			renderBox = new VBox();
			renderBox.verticalGap = 0;
			addChild(renderBox);
		}
		override protected function reflashRender():void
		{
			var startIndex:int = scrollItemIndex;
			for(var i:int=0,len:int=renders.length;i<len;i++)
			{
				renders[i].listData = (i+startIndex>itemnum-1) ? null : getListData(items[i+startIndex]);
				renders[i].data = (i+startIndex>itemnum-1) ? null : items[i+startIndex];
			}
			
			super.reflashRender();
		}
		
		//-----------------------------------------------------------------
		//  scrollbar
		//-----------------------------------------------------------------
		override protected function reflashVScrollBar():void
		{
			super.reflashVScrollBar();
			vScrollbar.x = width - vScrollbar.width;
			vScrollbar.height = height;
			vScrollbar.maxScrollPosition = itemnum * itemHeight;
			vScrollbar.viewarea = new Rectangle(0,0,width,height);
			vScrollbar.snapInterval = itemnum*itemHeight/validScrollItemNum;
		}
		//--------------------------------------------------------------------------
		//
		//  public Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  override BaseList
		//-----------------------------------------------------------------
		override public function addItemAt(item:Object, index:int):void
		{
			super.addItemAt(item,index);
			if(index>=0 && index<=items.length)
				addListData(item);
		}
		
		override public function removeItemAt(index:int):void
		{
			if(index>=0 && index<items.length)
				removeListData(items[index]);
			super.removeItemAt(index);
		}
		
		override public function removeAll():void
		{
			for(var z:Object in listDatas)
				delete listDatas[z];
			super.removeAll();
		}
		
		//--------------------------------------------------------------------------
		//
		//  utils method
		//
		//--------------------------------------------------------------------------
		override protected function isNeedReflashRender(index:int):Boolean
		{
			return index<scrollItemIndex+renders.length+2
		}
		override protected function isNeedReCreateRender():Boolean
		{
			var len:int = Math.min(itemnum,maxRenderNum);
			len = Math.max(1,len);
			return renders.length != len;
		}
		//========================= end =========================
	}
}